package learning.socket4;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.util.List;

public class Sent_msg extends Thread {
	List<Socket> list;
	Socket s;

	public Sent_msg(List<Socket> list, Socket s) {
		super();
		this.list = list;
		this.s = s;
	}

	@Override
	public void run() {
		int count = 0;
		String name = "";
		try {
			BufferedReader br = new BufferedReader(new InputStreamReader(
					s.getInputStream()));
			while (true) {
				String receive = br.readLine();
				if (count == 0) { // judge whether it is readed once
					name = receive;
					System.out.println(receive + " join us");
					count = -1;
				} else {
					for (Socket sk : list) {// sent information to all persons
						BufferedWriter bw = new BufferedWriter(
								new OutputStreamWriter(sk.getOutputStream()));
						bw.write(name + ":" + receive);
						bw.newLine();
						bw.flush();
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
